import java.util.Deque;
import java.util.LinkedList;

public class DecodeString {

    public static void main(String[] args) {
        DecodeString decodeString = new DecodeString();
        System.out.println(decodeString.decodeString("100[leetcode]"));
    }

    public String decodeString(String s) {
        Deque<String> stackS = new LinkedList<>();
        Deque<Integer> stackI = new LinkedList<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < s.length(); i++) {
            char c = chars[i];
            if (c >= '0' && c <= '9') {
                StringBuilder temp = new StringBuilder();
                while (chars[i] >= '0' && chars[i] <= '9') {
                    temp.append(chars[i++]);
                }
                --i;
                stackI.push(Integer.parseInt(temp.toString()));
            } else if (c != ']') {
                stackS.push(c + "");
            } else {
                StringBuilder stringBuilder = new StringBuilder();
                while (!stackS.peek().equals("[")) {
                    stringBuilder.append(stackS.poll());
                }
                stackS.poll();
                String temp = stringBuilder.toString();
                Integer count = stackI.poll();
                for (int j = 1; j < count; j++) {
                    stringBuilder.append(temp);
                }
                stackS.push(stringBuilder.toString());
            }
        }
        StringBuilder ans = new StringBuilder();
        while (!stackS.isEmpty()) {
            ans.append(stackS.poll());
        }
        return ans.reverse().toString();
    }
}
